package com.egg.ylt.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.GlideCircleTransform;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.activity.ACT_AboutUs;
import com.egg.ylt.activity.ACT_CallCenter;
import com.egg.ylt.activity.ACT_CollectCentre;
import com.egg.ylt.activity.ACT_Coupons;
import com.egg.ylt.activity.ACT_DeviceManagement;
import com.egg.ylt.activity.ACT_Family;
import com.egg.ylt.activity.ACT_Integral;
import com.egg.ylt.activity.ACT_MessageCenter;
import com.egg.ylt.activity.ACT_OpinionCoupleBack;
import com.egg.ylt.activity.ACT_Order;
import com.egg.ylt.activity.ACT_UserInfo;
import com.egg.ylt.activity.ACT_UserLogin;
import com.egg.ylt.activity.ACT_UserSetting;
import com.egg.ylt.activity.ACT_UserTimeCard;
import com.egg.ylt.activity.ACT_Wallet;
import com.egg.ylt.pojo.UpLoadFileEntity;
import com.egg.ylt.pojo.UserByidEntity;
import com.egg.ylt.presenter.impl.CenterPresenterImpl;
import com.egg.ylt.view.ICenterView;
import com.ruffian.library.widget.RImageView;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ScreenUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sichaoqi on 2018/8/23.
 */

public class FRA_Center extends BaseFragment<CenterPresenterImpl> implements ICenterView {
    @BindView(R.id.center_iv_set)
    ImageView centerIvSet;
    @BindView(R.id.center_iv_message)
    ImageView centerIvMessage;
    @BindView(R.id.center_iv_photo)
    RImageView centerIvPhoto;
    @BindView(R.id.center_tv_user_name)
    TextView centerTvUserName;
    @BindView(R.id.center_tv_user_phone)
    TextView centerTvUserPhone;
    @BindView(R.id.center_rl_view)
    RelativeLayout centerLlTopView;
    @BindView(R.id.center_ll_view)
    RelativeLayout centerLlView;
    @BindView(R.id.tv_collect_num)
    TextView tvCollectNum;
    @BindView(R.id.tv_coupon_num)
    TextView tvCouponNum;
    @BindView(R.id.tv_integral)
    TextView tvIntegral;
    @BindView(R.id.iv_update)
    ImageView ivUpdate;

    private AppSharedPreferences mSp;


    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void showException(String msg) {
        centerIvPhoto.setImageResource(R.mipmap.ic_mother);
        centerTvUserName.setText("未登录");
        ivUpdate.setVisibility(View.GONE);
        centerTvUserPhone.setText("");
        //优惠卷
        tvCouponNum.setText("0");
        //收藏
        tvCollectNum.setText("0");
        //积分
        tvIntegral.setText("0");
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {
        mPresenter.getUserInfo(Constants.TOKEN);
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getUserInfo(Constants.TOKEN);
        }
    }

    @Override
    protected View getLoadingTargetView() {
        return centerLlView;
    }

    @Override
    protected void initViewsAndEvents() {
        mSp = new AppSharedPreferences(mContext);


        if (StringUtil.isEmpty(Constants.TOKEN)) {
            centerIvPhoto.setImageResource(R.mipmap.ic_mother);
            ivUpdate.setVisibility(View.GONE);

            centerTvUserName.setText("未登录");
            centerTvUserPhone.setText("");
            //优惠卷
            tvCouponNum.setText("0");
            //收藏
            tvCollectNum.setText("0");
            //积分
            tvIntegral.setText("0");
        } else {
            mPresenter.getUserInfo(Constants.TOKEN);
        }

    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_center;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.EXIT_LOGIN_GO_HOME:
                centerIvPhoto.setImageResource(R.mipmap.ic_mother);
                ivUpdate.setVisibility(View.GONE);
                centerTvUserName.setText("未登录");
                centerTvUserPhone.setText("");
                //优惠卷
                tvCouponNum.setText("0");
                //收藏
                tvCollectNum.setText("0");
                //积分
                tvIntegral.setText("0");
                break;
        }
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    /**
     * 界面中控件的点击事件
     */
    @OnClick({R.id.center_iv_photo, R.id.center_tv_user_name, R.id.center_tv_user_phone, R.id.center_iv_set,
            R.id.center_iv_message, R.id.iv_update,
            R.id.ll_coupon, R.id.ll_collection, R.id.ll_integral,
            R.id.ll_family, R.id.ll_order, R.id.ll_wallet,
            R.id.ll_time_card, R.id.ll_equipment, R.id.ll_opinion,
            R.id.ll_service, R.id.ll_about_us})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.center_iv_photo://用户头像
            case R.id.center_tv_user_name:
            case R.id.center_tv_user_phone://去个人中心
            case R.id.iv_update://修改按钮
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_UserInfo.class);
                break;
            case R.id.center_iv_set://设置
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_UserSetting.class);
                break;
            case R.id.center_iv_message://去消息中心
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_MessageCenter.class);
                break;
            case R.id.ll_coupon://优惠卷
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_Coupons.class);
                break;
            case R.id.ll_collection://收藏
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_CollectCentre.class);
                break;
            case R.id.ll_integral://积分
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_Integral.class);
                break;

            case R.id.ll_family://亲友团
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                ACT_Family.startAc(mContext);
                break;
            case R.id.ll_order://订单
                // TODO: 2019/1/18 订单调试
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                ACT_Order.startAc(mContext);
                break;
            case R.id.ll_wallet://钱包
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_Wallet.class);
                break;

            case R.id.ll_time_card://次卡
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_UserTimeCard.class);
                break;
            case R.id.ll_equipment://设备管理
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_DeviceManagement.class);
                break;

            case R.id.ll_opinion://意见反馈
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                readyGo(ACT_OpinionCoupleBack.class);
                break;
            case R.id.ll_service://客服中心
                readyGo(ACT_CallCenter.class);
                break;
            case R.id.ll_about_us://关于我们
                readyGo(ACT_AboutUs.class);
                break;

        }
    }

    /**
     * 获取用户基础信息
     *
     * @param entity
     */
    @Override
    public void getUserInfo(UserByidEntity entity) {
        if (entity == null) {
            centerIvPhoto.setImageResource(R.mipmap.ic_mother);
            centerTvUserName.setText("未设置");
            centerTvUserPhone.setText("未设置");

            return;
        }
        ivUpdate.setVisibility(View.VISIBLE);

        //用户头像
        Glide.with(mContext)
                .load(entity.getHeadImage())
                .error(R.mipmap.ic_mother)
                .fallback(R.mipmap.ic_mother)
                .transform(new GlideCircleTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .into(centerIvPhoto);

        //用户昵称
        if (StringUtil.isEmpty(entity.getName())) {
            centerTvUserName.setText("未设置");
        } else {
            centerTvUserName.setText(entity.getName());
        }

        //用户手机号
        mSp.putString(Constants.USER_PHONE, entity.getPhone());
        centerTvUserPhone.setText(CustomUtils.setPhoneHide(entity.getPhone()));
        //优惠卷
        tvCouponNum.setText(entity.getTicket());
        //收藏
        tvCollectNum.setText(entity.getCollect());
        //积分
        tvIntegral.setText(entity.getFlow());
    }

    @Override
    public void onPostPhoto() {

    }

    @Override
    public void getImgUri(UpLoadFileEntity entity) {

    }

}
